<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
       <el-form-item label="序号" prop="serialNumber">
        <el-input v-model="formData.serialNumber" placeholder="请输入序号" />
      </el-form-item>
      <el-form-item label="部件" prop="component">
        <el-input v-model="formData.component" placeholder="请输入部件" />
      </el-form-item>

      <el-form-item label="用料来源" prop="materialSource">
        <el-select v-model="formData.productType" placeholder="请选择用料来源">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.SOURCE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="材料类型" prop="materialType">
        <el-select v-model="formData.materialType" placeholder="请选择用料来源">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.MATERIAL_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="宽公式" prop="widthFormula">
        <el-input
          ref="widthFormulaRef"
          v-model="formData.widthFormula"
          readonly
          @dblclick="openCalculator('widthFormula', widthFormulaRef)"
          style="cursor: pointer;"
        />
      </el-form-item>

      <el-form-item label="长公式" prop="lengthFormula">
        <el-input
          ref="lengthFormulaRef"
          v-model="formData.lengthFormula"
          readonly
          @dblclick="openCalculator('lengthFormula', lengthFormulaRef)"
          style="cursor: pointer;"
        />
      </el-form-item>

      <el-form-item label="钉条" prop="nailStrip">
        <el-input
          ref="nailStripRef"
          v-model="formData.nailStrip"
          readonly
          @dblclick="openCalculator('nailStrip', nailStripRef)"
          style="cursor: pointer;"
        />
      </el-form-item>
      <el-form-item label="数量比" prop="quantityRatio">
        <el-input v-model="formData.quantityRatio" placeholder="请输入数量比" />
      </el-form-item>
      <el-form-item label="压线类型" prop="pressLineTypeId">
        <el-select v-model="formData.pressLineTypeId" placeholder="请选择压线类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>

      <el-form-item label="压线一" prop="pressLineOne">
        <el-input
          ref="pressLineOneRef"
          v-model="formData.pressLineOne"
          readonly
          @dblclick="openCalculator('pressLineOne', pressLineOneRef)"
          style="cursor: pointer;"
        />
      </el-form-item>

      <el-form-item label="压线二" prop="pressLineTwo">
        <el-input
          ref="pressLineTwoRef"
          v-model="formData.pressLineTwo"
          readonly
          @dblclick="openCalculator('pressLineTwo', pressLineTwoRef)"
          style="cursor: pointer;"
        />
      </el-form-item>

      <el-form-item label="压线三" prop="pressLineThree">
        <el-input
          ref="pressLineThreeRef"
          v-model="formData.pressLineThree"
          readonly
          @dblclick="openCalculator('pressLineThree', pressLineThreeRef)"
          style="cursor: pointer;"
        />
      </el-form-item>

      <el-form-item label="开槽一" prop="slotOne">
        <el-input
          ref="slotOneRef"
          v-model="formData.slotOne"
          readonly
          @dblclick="openCalculator('slotOne', slotOneRef)"
          style="cursor: pointer;"
        />
      </el-form-item>

      <el-form-item label="开槽二" prop="slotTwo">
        <el-input
          ref="slotTwoRef"
          v-model="formData.slotTwo"
          readonly
          @dblclick="openCalculator('slotTwo', slotTwoRef)"
          style="cursor: pointer;"
        />
      </el-form-item>

      <el-form-item label="开槽三" prop="slotThree">
        <el-input
          ref="slotThreeRef"
          v-model="formData.slotThree"
          readonly
          @dblclick="openCalculator('slotThree', slotThreeRef)"
          style="cursor: pointer;"
        />
      </el-form-item>

      <el-form-item label="开槽四" prop="slotFour">
        <el-input
          ref="slotFourRef"
          v-model="formData.slotFour"
          readonly
          @dblclick="openCalculator('slotFour', slotFourRef)"
          style="cursor: pointer;"
        />
      </el-form-item>

      <el-form-item label="开槽五" prop="slotFive">
        <el-input
          ref="slotFiveRef"
          v-model="formData.slotFive"
          readonly
          @dblclick="openCalculator('slotFive', slotFiveRef)"
          style="cursor: pointer;"
        />
      </el-form-item>

      <el-form-item label="压线四" prop="pressLineFour">
        <el-input
          ref="pressLineFourRef"
          v-model="formData.pressLineFour"
          readonly
          @dblclick="openCalculator('pressLineFour', pressLineFourRef)"
          style="cursor: pointer;"
        />
      </el-form-item>

      <el-form-item label="压线五" prop="pressLineFive">
        <el-input
          ref="pressLineFiveRef"
          v-model="formData.pressLineFive"
          readonly
          @dblclick="openCalculator('pressLineFive', pressLineFiveRef)"
          style="cursor: pointer;"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注" />
      </el-form-item>
      <el-form-item label="用料默认单位" prop="defaultMaterialUnit">
        <el-input v-model="formData.defaultMaterialUnit" placeholder="请输入用料默认单位" />
      </el-form-item>
      <el-form-item label="用料材质" prop="material">
        <el-input v-model="formData.material" placeholder="请输入用料材质" />
      </el-form-item>
      <el-form-item label="印刷预损" prop="printPreLoss">
        <el-radio-group v-model="formData.printPreLoss">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="后工序预损" prop="postProcessPreLoss">
        <el-radio-group v-model="formData.postProcessPreLoss">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="同款单序号" prop="sameOrderSerial">
        <el-input v-model="formData.sameOrderSerial" placeholder="请输入同款单序号" />
      </el-form-item>
      <el-form-item label="默认用料报价单位" prop="defaultMaterialPriceUnit">
        <el-input v-model="formData.defaultMaterialPriceUnit" placeholder="请输入默认用料报价单位" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>

  <!-- 计算器弹窗 -->
  <CalculatorDialog ref="calculatorRef" />
  <CalculatorDialog1 ref="calculatorRef1" />
</template>
<script setup lang="ts">
import { BaseBoxTypeApi } from '@/api/erp/baseboxtype'
import { getStrDictOptions, getBoolDictOptions, DICT_TYPE } from '@/utils/dict'
import CalculatorDialog from "@/components/CalculatorDialog/CalculatorDialog.vue";
import CalculatorDialog1 from "@/components/CalculatorDialog/CalculatorDialog1.vue";

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  boxTypeId: undefined,
  serialNumber: undefined,
  component: undefined,
  materialSource: undefined,
  materialType: undefined,
  widthFormula: undefined,
  lengthFormula: undefined,
  nailStrip: undefined,
  quantityRatio: undefined,
  pressLineTypeId: undefined,
  pressLineOne: undefined,
  pressLineTwo: undefined,
  pressLineThree: undefined,
  slotOne: undefined,
  slotTwo: undefined,
  slotThree: undefined,
  slotFour: undefined,
  slotFive: undefined,
  pressLineFour: undefined,
  pressLineFive: undefined,
  remark: undefined,
  defaultMaterialUnit: undefined,
  material: undefined,
  printPreLoss: undefined,
  postProcessPreLoss: undefined,
  sameOrderSerial: undefined,
  defaultMaterialPriceUnit: undefined
})
const formRules = reactive({
  boxTypeId: [{ required: true, message: '箱型 ID 关联 base_box_type.id不能为空', trigger: 'blur' }],
  printPreLoss: [{ required: true, message: '印刷预损不能为空', trigger: 'blur' }],
  postProcessPreLoss: [{ required: true, message: '后工序预损不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number, boxTypeId: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  formData.value.boxTypeId = boxTypeId
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await BaseBoxTypeApi.getBaseBoxMaterialFormula(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value
    if (formType.value === 'create') {
      await BaseBoxTypeApi.createBaseBoxMaterialFormula(data)
      message.success(t('common.createSuccess'))
    } else {
      await BaseBoxTypeApi.updateBaseBoxMaterialFormula(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    boxTypeId: undefined,
    serialNumber: undefined,
    component: undefined,
    materialSource: undefined,
    materialType: undefined,
    widthFormula: undefined,
    lengthFormula: undefined,
    nailStrip: undefined,
    quantityRatio: undefined,
    pressLineTypeId: undefined,
    pressLineOne: undefined,
    pressLineTwo: undefined,
    pressLineThree: undefined,
    slotOne: undefined,
    slotTwo: undefined,
    slotThree: undefined,
    slotFour: undefined,
    slotFive: undefined,
    pressLineFour: undefined,
    pressLineFive: undefined,
    remark: undefined,
    defaultMaterialUnit: undefined,
    material: undefined,
    printPreLoss: undefined,
    postProcessPreLoss: undefined,
    sameOrderSerial: undefined,
    defaultMaterialPriceUnit: undefined
  }
  formRef.value?.resetFields()
}

// 打开计算器
const calculatorRef = ref(null);
// 打开计算器
const openCalculator = (field, inputRef) => {
  calculatorRef.value.open((value) => {
    formData.value[field] = value;
  }, inputRef);
};

const calculatorRef1 = ref(null);
const openCalculator1 = (field, inputRef) => {
  calculatorRef1.value.open((value) => {
    formData.value[field] = value;
  }, inputRef);
};
</script>
